﻿using GN.Pay.Utils.Security;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace GN.Pay.Utils
{
    public static class PreParam
    {
        public static string BuildRequestParam(Dictionary<string, string> dicArray, string _input_charset)
        {
            StringBuilder builder = new StringBuilder();
            foreach (KeyValuePair<string, string> pair in dicArray)
            {
                builder.Append(pair.Key + "=" + HttpUtility.UrlEncode(pair.Value, Encoding.GetEncoding(_input_charset)) + "&");
            }
            int length = builder.Length;
            builder.Remove(length - 1, 1);
            return builder.ToString();
        }

        public static Dictionary<string, string> FilterPara(Dictionary<string, string> dicPara, string[] ignorKeys)
        {
            var dictionary = new Dictionary<string, string>();
            var sortedDictionary = new SortedDictionary<string, string>(dicPara);
            foreach (KeyValuePair<string, string> pair in sortedDictionary)
            {
                if (!(ignorKeys.Contains(pair.Key.ToLower()) || string.IsNullOrEmpty(pair.Value)))
                {
                    dictionary.Add(pair.Key, pair.Value);
                }
            }
            return dictionary;
        }

        /// <summary>
        /// 过滤为空的参数 进行排序
        /// </summary>
        /// <param name="dicPara"></param>
        /// <returns></returns>
        public static Dictionary<string, string> FilterPara(Dictionary<string, string> dicPara)
        {
            var dictionary = new Dictionary<string, string>();
            var sortedDictionary = new SortedDictionary<string, string>(dicPara);
            foreach (KeyValuePair<string, string> pair in sortedDictionary)
            {
                if (!string.IsNullOrEmpty(pair.Value))
                {
                    dictionary.Add(pair.Key, pair.Value);
                }
            }
            return dictionary;
        }
        /// <summary>
        /// key参数为空时进行MD5加密
        /// </summary>
        /// <param name="prestr"></param>
        /// <param name="_input_charset"></param>
        /// <param name="_sign_type"></param>
        /// <param name="_key"></param>
        /// <returns></returns>
        public static string BuildRequestMysign(string prestr, string _input_charset, string _sign_type, string _key = "")
        {
            switch (_sign_type.ToUpper())
            {
                case "MD5":
                    return Md5Utils.Sign(prestr, Encoding.GetEncoding(_input_charset));
                case "RSA":
                    return RsaFromPkcs8Utils.Sign(prestr, _key, Encoding.GetEncoding(_input_charset));
            }
            return "";
        }

        /// <summary>
        /// 拼接字符串
        /// </summary>
        /// <param name="dicArray"></param>
        /// <returns></returns>
        public static string CreateLinkString(Dictionary<string, string> dicArray)
        {
            var builder = new StringBuilder();
            foreach (KeyValuePair<string, string> pair in dicArray)
            {
                builder.Append(pair.Key + "=" + pair.Value + "&");
            }
            int length = builder.Length;
            builder.Remove(length - 1, 1);
            return builder.ToString();
        }

        public static NameValueCollection GetQueryString(HttpRequestBase request)
        {
            if ("POST".Equals(request.HttpMethod, StringComparison.InvariantCultureIgnoreCase))
            {
                return request.Form;
            }
            return request.QueryString;
        }

        /// <summary>
        /// 获取查询字典
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static IDictionary<string, string> GetQueryDictionary(HttpRequestBase request)
        {
            NameValueCollection requestPara = GetQueryString(request);
            Dictionary<string, string> inputDic = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
            foreach (string str in requestPara.AllKeys)
            {
                if (str != null)
                {
                    inputDic.Add(str, requestPara[str]);
                }
            }
            return inputDic;
        }        
    }
}
